import { createRouter, createWebHistory } from "vue-router";
import Home from "../views/Home.vue";
import checkUserIsLogin from "@/utils/oidc";

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "home",
    component: Home,
  },
  {
    path: "/oidc/redirectPage",
    name: "redirectPage",
    component: import("../views/oidc/redirectPage.vue"),
  },
  {
    path: "/oidc/redirectLogout",
    name: "redirectLogout",
    component: import("../views/oidc/redirectLogout.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

router.beforeEach(async (to, from, next) => {
  console.log("主应用 - 前置守卫", to);
  if (to.name === "redirectPage") {
    next();
  } else if (to.name === "redirectLogout") {
    next();
  } else {
    try {
      await checkUserIsLogin(true);
    } catch (err) {
      console.log("检查到用户未登录", err);
    }
    next();
  }
});

export default router;
